package org.bet.io;

import java.net.MalformedURLException;
import java.net.URL;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.UrlResource;
import org.springframework.util.ClassUtils;

public class CustomResourceLoader implements ResourceLoader {

	private ClassLoader classLoader;
	
	public CustomResourceLoader(){
		this(ClassUtils.getDefaultClassLoader());
	}
	
	public CustomResourceLoader(ClassLoader classLoader) {
		this.classLoader = classLoader;
		
	}

	public ClassLoader getClassLoader() {
		return classLoader;
	}

	public Resource getResource(String location) {
		try {
			URL url = new URL(location);
			return new UrlResource(url);
		}
		catch (MalformedURLException ex) {
			if(location.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)){
				return new ClassPathResource(location);
			}
			else{
				return new StringResource(location);
			}
		}
	}
}
